﻿
Public Class color
    Public Shared ReadOnly black As color = New color("000000")
    Public Shared ReadOnly silver As color = New color("c0c0c0")
    Public Shared ReadOnly gray As color = New color("808080")
    Public Shared ReadOnly white As color = New color("ffffff")
    Public Shared ReadOnly maroon As color = New color("800000")
    Public Shared ReadOnly red As color = New color("ff0000")
    Public Shared ReadOnly purple As color = New color("800080")
    Public Shared ReadOnly fuchsia As color = New color("ff00ff")
    Public Shared ReadOnly green As color = New color("008000")
    Public Shared ReadOnly lime As color = New color("00ff00")
    Public Shared ReadOnly olive As color = New color("808000")
    Public Shared ReadOnly yellow As color = New color("ffff00")
    Public Shared ReadOnly navy As color = New color("000080")
    Public Shared ReadOnly blue As color = New color("0000ff")
    Public Shared ReadOnly teal As color = New color("008080")
    Public Shared ReadOnly aqua As color = New color("00ffff")

    'following are the colors not supported in rfc
    Public Shared ReadOnly windowframe As color = New color("F5F5F5")

    Protected _r As Byte
    Protected _g As Byte
    Protected _b As Byte

    Public Function setcolor(ByRef rgb As String) As Boolean
        Dim len As Int64
        Dim eachlen As Int32
        len = strlen(rgb)
        If len <= 3 Then
            eachlen = 1
            strfill(rgb, Global.character._0, 3)
        Else
            eachlen = 2
            strfill(rgb, Global.character._0, 6)
        End If

        Return convertor.tobyte(strmid(rgb, 0 * eachlen, eachlen), _r, 16) AndAlso _
                convertor.tobyte(strmid(rgb, 1 * eachlen, eachlen), _g, 16) AndAlso _
                convertor.tobyte(strmid(rgb, 2 * eachlen, eachlen), _b, 16)
    End Function

    Public Function setcolor(ByVal rgb As Int32) As Boolean
        Dim bs As bytes = Nothing
        If convertor.tobytes(rgb, bs) AndAlso bs.size = 4 Then
            _r = bs(1)
            _g = bs(2)
            _b = bs(3)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function setcolor(ByVal r As Byte, ByVal g As Byte, ByVal b As Byte) As Boolean
        _r = r
        _g = g
        _b = b

        Return True
    End Function

    Public Overrides Function ToString() As String
        Return Global.character.hashMark _
                + strrfill(Hex(_r), Global.character._0, 2) _
                + strrfill(Hex(_g), Global.character._0, 2) _
                + strrfill(Hex(_b), Global.character._0, 2)
    End Function

    Public Sub New(ByRef rgb As String)
        setcolor(rgb)
    End Sub

    Public Sub New(ByVal rgb As Int32)
        setcolor(rgb)
    End Sub

    Public Sub New(ByVal r As Byte, ByVal g As Byte, ByVal b As Byte)
        setcolor(r, g, b)
    End Sub

    Public Sub New()
        setcolor(0, 0, 0)
    End Sub

    Public Shared Sub unittest()
        Dim c As color = Nothing
        c = New color
        If c.setcolor("abcdef") Then
            Console.WriteLine(c.ToString())
        Else
            raiseError("cannot convert ""abcdef"" to color", errorHandle.errorType.exclamation)
        End If
        If c.setcolor(1, 1, 1) Then
            Console.WriteLine(c.ToString())
        Else
            raiseError("cannot convert 1,1,1 to color", errorHandle.errorType.exclamation)
        End If
        If c.setcolor("mmmmmm") Then
            Console.WriteLine(c.ToString())
        Else
            raiseError("cannot convert ""mmmmmm"" to color", errorHandle.errorType.exclamation)
        End If
    End Sub
End Class
